package drds.data_propagate.store;

import com.google.protobuf.ByteString;
import drds.data_propagate.common.utils.ToStringStyle;
import drds.propagate.protocol.CanalEntry;
import drds.propagate.protocol.CanalEntry.EntryType;
import drds.propagate.protocol.CanalEntry.EventType;
import drds.propagate.protocol.Entry;
import drds.propagate.protocol.position.LogId;

import java.io.Serializable;
import java.util.List;

/**
 * store存储数据对象
 *
 * @author jianghang 2012-7-13 下午03:03:03
 */
public class Event implements Serializable {

    private static final long serialVersionUID = 1333330351758762739L;

    private LogId logId;                            // 记录数据产生的来源
    private ByteString rawEntry;

    private long executeTime;
    private EntryType entryType;
    private String journalName;
    private long position;
    private long serverId;
    private EventType eventType;
    private String gtid;
    private long rawLength;
    private int rowsCount;

    // ==== https://github.com/alibaba/canal/issues/1019
    private Entry entry;

    public Event() {
    }

    public Event(LogId logId, Entry entry) {
        this(logId, entry, true);
    }

    public Event(LogId logId, Entry entry, boolean raw) {
        this.logId = logId;
        this.entryType = entry.getEntryType();
        this.executeTime = entry.getHeader().getExecuteTime();
        this.journalName = entry.getHeader().getLogfileName();
        this.position = entry.getHeader().getLogfileOffset();
        this.serverId = entry.getHeader().getServerId();
        this.gtid = entry.getHeader().getGtid();
        this.eventType = entry.getHeader().getEventType();
        if (entryType == EntryType.row_data) {
            List<CanalEntry.Pair> props = entry.getHeader().getPropsList();
            if (props != null) {
                for (CanalEntry.Pair p : props) {
                    if ("rowsCount".equals(p.getKey())) {
                        rowsCount = Integer.parseInt(p.getValue());
                        break;
                    }
                }
            }
        }

        if (raw) {
            // build raw
            this.rawEntry = entry.toByteString();
            this.rawLength = rawEntry.size();
        } else {
            this.entry = entry;
            // 按照6倍的event length预估
            this.rawLength = entry.getHeader().getEventLength() * 6;
        }
    }

    public LogId getLogId() {
        return logId;
    }

    public void setLogId(LogId logId) {
        this.logId = logId;
    }

    public ByteString getRawEntry() {
        return rawEntry;
    }

    public void setRawEntry(ByteString rawEntry) {
        this.rawEntry = rawEntry;
    }

    public long getExecuteTime() {
        return executeTime;
    }

    public void setExecuteTime(long executeTime) {
        this.executeTime = executeTime;
    }

    public EntryType getEntryType() {
        return entryType;
    }

    public void setEntryType(EntryType entryType) {
        this.entryType = entryType;
    }

    public String getJournalName() {
        return journalName;
    }

    public void setJournalName(String journalName) {
        this.journalName = journalName;
    }

    public long getPosition() {
        return position;
    }

    public void setPosition(long position) {
        this.position = position;
    }

    public long getServerId() {
        return serverId;
    }

    public void setServerId(long serverId) {
        this.serverId = serverId;
    }

    public String getGtid() {
        return gtid;
    }

    public void setGtid(String gtid) {
        this.gtid = gtid;
    }

    public long getRawLength() {
        return rawLength;
    }

    public void setRawLength(long rawLength) {
        this.rawLength = rawLength;
    }

    public EventType getEventType() {
        return eventType;
    }

    public void setEventType(EventType eventType) {
        this.eventType = eventType;
    }

    public int getRowsCount() {
        return rowsCount;
    }

    public void setRowsCount(int rowsCount) {
        this.rowsCount = rowsCount;
    }

    public Entry getEntry() {
        return entry;
    }

    public void setEntry(Entry entry) {
        this.entry = entry;
    }

    public String toString() {
        return ToStringStyle.toString(this);
    }

}
